<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: mercari_img.php 182 2013-09-22 07:56:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 182 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:56:23 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function hex2dec($hex) {
	$hex = str_replace('#', '', $hex);
	$ret = array('r' => hexdec(substr($hex, 0, 2)), 'g' => hexdec(substr($hex, 2, 2)), 'b' => hexdec(substr($hex, 4, 2)));
	return $ret;
}

header("Cache-Control: public, must-revalidate, max-age=3600, post-check=3600, pre-check=3600");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header('Expires: '.gmdate('D, d M Y H:i:s', time()+31536000).' GMT');
header("Content-type:text/javascript;charset:UTF-8");
header('Connection: Keep-Alive'); 
header('Keep-Alive: timeout=300');

if(isset($_GET['name']) && !empty($_GET['name'])) {
	if($_GET['s'] == 'jpg') {
		$image = ImageCreateFromJPEG('templates/'.$_GET['tpl'].'/img/'.$_GET['name'].'.jpg');
		header("Content-Type: image/jpeg");
		imageJPEG($image);

	} elseif ($_GET['s'] == 'png') {
		$image = imagecreatefrompng('templates/'.$_GET['tpl'].'/img/'.$_GET['name'].'.png');
		imagealphablending($image, true);
		imagesavealpha($image, true);
		header("Content-Type: image/png");
		imagePNG($image);

	} elseif ($_GET['s'] == 'gif') {
		$image = imagecreatefromgif('templates/'.$_GET['tpl'].'/img/'.$_GET['name'].'.gif');
		header("Content-Type: image/gif");
		imageGIF($image);
	}

} else {
	$w = (isset($_GET['w']) && !empty($_GET['w']) ? $_GET['w'] : '1');
	$h = (isset($_GET['h']) && !empty($_GET['h']) ? $_GET['h'] : '30');
	$gc1 = (isset($_GET['c1']) && !empty($_GET['c1']) ? $_GET['c1'] : 'ffffff');
	$gc2 = (isset($_GET['c2']) && !empty($_GET['c2']) ? $_GET['c2'] : '000000');
	$c1  = hex2dec($gc1);
	$c2  = hex2dec($gc2);
	
	$image  = imagecreate($w, $h);
	
	if(isset($_GET['c3']) && isset($_GET['c4'])) {
		$h_h = floor($h/2);
		$h_f = $h;
	} else
		$h_h = $h;
	
	for($i = 0; $i <= $h_h+1; $i++) {
	    $l1 = ImageColorAllocate($image,$c1['r']-((($c2['r']-$c1['r'])/-$h_h)*$i),
										$c1['g']-((($c2['g']-$c1['g'])/-$h_h)*$i),
										$c1['b']-((($c2['b']-$c1['b'])/-$h_h)*$i));
										
	    imageline($image, 0, $i, $w, $i, $l1);
	}
	
	if(isset($_GET['c3']) && isset($_GET['c4'])) {
		$c3  = hex2dec($_GET['c3']);
		$c4  = hex2dec($_GET['c4']);
		
		for($i = 0; $i <= $h_f+1; $i++) {
		    $l1 = ImageColorAllocate($image,$c3['r']-((($c4['r']-$c3['r'])/-($h_h))*$i),
							                $c3['g']-((($c4['g']-$c3['g'])/-($h_h))*$i),
							                $c3['b']-((($c4['b']-$c3['b'])/-($h_h))*$i));
		    imageline($image, 0, $i+$h_h, $w, $i+$h_h, $l1);
		}
	}
	
	if(isset($_GET['d']) && !empty($_GET['d'])) // Rotation
		$image = imagerotate($image, (int)$_GET['d'], 0);
	
	header("Content-type: image/jpeg");
	imageJPEG($image);
}
echo $image;
imagedestroy($image);